<?php

class Model{
        
    protected $db;

    public function __construct(){
        #$this->db = new PDO("mysql:host=localhost;dbname=jbfm","hpjbfm","jbfm0007");
        #$this->db = new PDO("mysql:host=mysql.intranet;dbname=jbfm","hpjbfm","jbfm0007");
        #$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $this->db = mysql_connect("localhost","eric_user","er0901");
                    mysql_select_db("eric_db",$this->db);
    }

    public function insert( $tabela, Array $dados){

        foreach ($dados as $indice => $val){
            $campos[] = $indice;
            $valores[] = $val;
        }

        # obtem os valores 
        $campos  = implode(", ", $campos);
        $valores = "'".implode("','", $valores)."'";
        
        # Monta a query 
        $query = mysql_query("INSERT INTO `{$tabela}` ({$campos}) VALUES({$valores});",$this->db);
        
            # retorna 
        return $query;
        
        
           

    }

    public function read( $tabela, $where, $order, $limite, $habilitar_cache ){

        # obtem os valores
        
        $where = ( ($where != null) ? "WHERE {$where}" : "" );

        $order = ( ($order != null) ? "ORDER BY {$order}" : "" );

        $limite = ( ($limite != null) ? "LIMIT {$limite}" : "" );
        $cache = ( ($habilitar_cache == null) ? "SQL_CACHE" : "" );
        #echo "SELECT * FROM `{$tabela}` {$where} {$order} {$limite} ;";
        
        try
        {
            $query = mysql_query("SELECT {$cache} * FROM `{$tabela}` {$where} {$order} {$limite} ;",$this->db);
            $params = mysql_fetch_assoc($query);
            
            # retorna 
            return $params;
            
            
           
        }
            catch(Exception $i){
                # se houver exceção, exibe
                # print "<code>" . $i->getMessage() . "</code>";
                return false;
            }
        
        
    }
    
    public function read_min_or_max( $tabela, $min_max, $campo, $where, $order, $limite, $habilitar_cache){


        $where = ( ($where != null) ? "WHERE {$where}" : "" );

        $order = ( ($order != null) ? "ORDER BY {$order}" : "" );

        $limite = ( ($limite != null) ? "LIMIT {$limite}" : "" );
        
        $cache = ( ($habilitar_cache == null) ? "SQL_CACHE" : "" );
        # echo "SELECT {$min_max}({$campo}) FROM `{$tabela}` {$where} {$order} {$limite} ;";
        try
        {
            $query = mysql_query("SELECT {$cache} {$min_max}({$campo}) FROM `{$tabela}` {$where}{$order}{$limite};",$this->db);
            $params = mysql_fetch_assoc($query);
            
            # retorna 
            return $params;

            
            
        }
            catch(Exception $i)
            {
                # se houver exceção, exibe
                # print "<code>" . $i->getMessage() . "</code>";
                return false;
            }
        
        
    }
    
    public function read_join($dados){
        try
        {
        
            $query = mysql_query("{$dados};",$this->db);  
            
            $params = mysql_fetch_assoc($query);

            # retorna
            return $params;
            
        
        }catch(Exception $i){
           # se houver exceção, exibe
           # print "<code>" . $i->getMessage() . "</code>";
            return false;
        }
    }
    
    


    public function update($tabela, Array $dados, $where){

        foreach($dados as $indice => $valor){
            $campos[] = "{$indice} = '{$valor}'";
        }

        $campos = implode(", ",$campos);
        #echo "UPDATE `{$tabela}` SET {$campos} WHERE {$where};";
        #exit;
        try
        {
        
            $query = mysql_query("UPDATE `{$tabela}` SET {$campos} WHERE {$where};",$this->db);
            
            # retorna
            return mysql_fetch_assoc($query);
            

        }catch(Exception $i){
           #se houver exceção, exibe
           print "<code>" . $i->getMessage() . "</code>";
        }

    }
    
    public function delete($tabela, $where){
        $query = $this->db->query("DELETE FROM `{$tabela}` WHERE {$where}; ",$this->db);
        
        /* retorna */
        return $query;        

    }
    


        
        
        
        
}
?>
